package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.endic.lib.model.Pronouncer;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyTransRecordBean;
import flc.ast.databinding.ActivityRecTransBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.base.IReqRetCallback;
import stark.common.other.LanCode;
import stark.common.other.ReqManager;
import stark.common.other.bean.TranslateRet;

public class RecTransActivity extends BaseAc<ActivityRecTransBinding> {

    public static String sResult;
    private LanCode mCurSelLanCodeFrom, mCurSelLanCodeTo;


    @Override
    protected int onCreate() {
        return R.layout.activity_rec_trans;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRecTransBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.tvFrom.setOnClickListener(this);
        mDataBinding.tvTo.setOnClickListener(this);
        mDataBinding.tvStart.setOnClickListener(this);
        mDataBinding.ivRead.setOnClickListener(this);
        mDataBinding.ivResRead.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvFrom://初始语言
                SelLanguageActivity.type = 0;
                SelLanguageActivity.mCurSelLanCodeFrom = mCurSelLanCodeFrom;
                SelLanguageActivity.mCurSelLanCodeTo = mCurSelLanCodeTo;
                Intent intent = new Intent(this, SelLanguageActivity.class);
                startActivityForResult(intent, Content.SEL_LAN);
                break;
            case R.id.tvTo://目标语言
                SelLanguageActivity.type = 1;
                SelLanguageActivity.mCurSelLanCodeFrom = mCurSelLanCodeFrom;
                SelLanguageActivity.mCurSelLanCodeTo = mCurSelLanCodeTo;
                Intent intent1 = new Intent(this, SelLanguageActivity.class);
                startActivityForResult(intent1, Content.SEL_LAN);
                break;
            case R.id.tvStart://开始翻译
                startTranslate();
                break;
            case R.id.ivRead://播放
                if (mCurSelLanCodeFrom != LanCode.EN) {
                    ToastUtils.showShort(R.string.not_support_chinese_playback);
                    return;
                }
                String speakText = mDataBinding.etInput.getText().toString();
                if (TextUtils.isEmpty(speakText)) return;
                Pronouncer.getInstance().playByEn(speakText);
                break;
            case R.id.ivResRead:
                if (mCurSelLanCodeTo != LanCode.EN) {
                    ToastUtils.showShort(R.string.not_support_chinese_playback);
                    return;
                }
                String speakText1 = mDataBinding.tvRes.getText().toString();
                if (TextUtils.isEmpty(speakText1)) return;
                Pronouncer.getInstance().playByEn(speakText1);
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.etInput.setText(sResult);
        mCurSelLanCodeFrom = LanCode.ZH;
        mCurSelLanCodeTo = LanCode.EN;
        mDataBinding.tvFrom.setText(mCurSelLanCodeFrom.getName());
        mDataBinding.tvTo.setText(mCurSelLanCodeTo.getName());
    }

    private void startTranslate() {
        showDialog(getString(R.string.translation_loading));
        ReqManager.getInstance().translate(this, mDataBinding.etInput.getText().toString(),
                mCurSelLanCodeFrom, mCurSelLanCodeTo, new IReqRetCallback<TranslateRet>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable TranslateRet result) {
                        if (result != null) {
                            mDataBinding.ivRead.setVisibility(View.VISIBLE);
                            mDataBinding.ivResRead.setVisibility(View.VISIBLE);
                            mDataBinding.llRes.setVisibility(View.VISIBLE);
                            mDataBinding.tvRes.setText(result.getResult());
                            List<MyTransRecordBean> listSp = AppSpUtils.getTransRecordList();
                            List<MyTransRecordBean> listSave = new ArrayList<>();
                            listSave.add(new MyTransRecordBean(mDataBinding.etInput.getText().toString(), result.getResult()));
                            if (listSp != null && listSp.size() != 0) {
                                listSp.addAll(listSave);
                                AppSpUtils.saveTransRecordList(listSp);
                            } else {
                                AppSpUtils.saveTransRecordList(listSave);
                            }
                        } else {
                            ToastUtils.showShort(msg);
                        }
                        dismissDialog();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == Content.SEL_LAN) {
                LanCode selLanCode = (LanCode) data.getSerializableExtra(Content.LAN_CODE);
                if (SelLanguageActivity.type == 0) {
                    mCurSelLanCodeFrom = selLanCode;
                    mDataBinding.tvFrom.setText(mCurSelLanCodeFrom.getName());
                } else {
                    mCurSelLanCodeTo = selLanCode;
                    mDataBinding.tvTo.setText(mCurSelLanCodeTo.getName());
                }
            }
        }
    }
}